//
//  UIBarButtonItem+Custom.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/14.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
    
    /// 便利构造函数, 在扩展中只能添加 --> 便利构造函数
    convenience init(imageName image:String,target: AnyObject?,action: Selector,touchStype:UIControlEvents) {
        let button = UIButton()
        button.setImage(UIImage(named: image), forState: UIControlState.Normal)
        button.setImage(UIImage(named: image + "_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()  // 让控件根据里面的内容是设置大小
        button.addTarget(target, action: action, forControlEvents: touchStype)
        self.init(customView:button)
    }
    convenience init(title:String,target: AnyObject?,action: Selector,touchStype:UIControlEvents) {
        let button = UIButton()
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.sizeToFit()  // 让控件根据里面的内容是设置大小
        button.addTarget(target, action: action, forControlEvents: touchStype)
        self.init(customView:button)
    }
    
    /// 创建带按钮的UIBarButtonItem, class表示类方法, 类名.方法名()
    class func addBarButtonWintImageName(imageName:String)->UIBarButtonItem{
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()  // 让控件根据里面的内容是设置大小
        
        return UIBarButtonItem(customView: button)
    }    
}













